package com.abe.bathsystem.base;

import android.support.annotation.IntDef;
import android.text.TextUtils;
import android.util.Base64;

import com.abe.bathsystem.entity.http.HttpRoomEntity;
import com.abe.bathsystem.utils.ConstantBath;
import com.abe.bathsystem.utils.NormalUtils;
import com.abe.bathsystem.utils.SerialPortUtils;
import com.abe.libcore.utils.Utils;
import com.abe.libcore.utils.normal.CommonUtils;
import com.abe.libcore.utils.normal.ConstantCore;
import com.abe.libcore.utils.normal.SPUtils;
import com.google.gson.Gson;

import java.io.File;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Type;
import java.util.Date;

public class ManagerLocalData {
    private static final String SP_NAME = "bath";
    public static final int LOCAL_TYPE_ROOM = 0x001;
    public static final int LOCAL_TYPE_LIFE = 0x002;

    @IntDef({LOCAL_TYPE_ROOM, LOCAL_TYPE_LIFE})
    @Retention(RetentionPolicy.SOURCE)
    @interface LocalDataType {

    }

    private static SPUtils spUtils() {
        return new SPUtils(SP_NAME);
    }

    public static void saveUpdateServerTime(Long time) {
        spUtils().putLong("update_server_time", time);
    }

    public static Long getUpdateServerTime() {
        return spUtils().getLong("update_server_time", new Date().getTime());
    }

    public static void updateAPP(boolean update_app) {
        spUtils().putBoolean("update_app", update_app);
    }

    public static boolean isUpdateAPP() {
        return spUtils().getBoolean("update_app", false);
    }

    //保存存活时间，防止断电
    public static void keepSurvival() {
        Date date = new Date();
        CommonUtils.w("存活时间记录:" + new Gson().toJson(date));
        saveData(date, LOCAL_TYPE_LIFE);
    }

    public static Date getOldDate() {
        return (Date) getLocalData(LOCAL_TYPE_LIFE, Date.class);
    }

    public static boolean isUseSerialPort() {
        return CommonUtils.getMetaBooleanData(Utils.getContext(), "UseSerialPort");
    }

    public static int getTestSerialPos() {
        return new SPUtils("bath").getInt("testSerialPos", 0);
    }

    public static void setTestSerialPos(int pos) {
        spUtils().putInt("testSerialPos", pos);
    }

    public static boolean isSupportLimitBath() {
        return spUtils().getBoolean("supportLimitBath", true);
    }

    public static void setIsSupportLimitBath(boolean support) {
        spUtils().putBoolean("supportLimitBath", support);
    }

    public static boolean isSupportLocker() {
        return spUtils().getBoolean("supportLocker", false);
    }

    public static void setIsSupportLocker(boolean supportLocker) {
        spUtils().putBoolean("supportLocker", supportLocker);
    }

    public static String getServiceUrl() {
        return spUtils().getString("serviceUrl", ConstantBath.ServiceUrl);
    }

    public static void setServiceUrl(String url) {
        if (TextUtils.isEmpty(url)) return;
        spUtils().putString("serviceUrl", url);
    }

    public static String getSerialPort() {
        return spUtils().getString("serialPortName", SerialPortUtils.path_ttys0);
    }

    public static void saveSerialPort(String name) {
        if (TextUtils.isEmpty(name)) return;
        spUtils().putString("serialPortName", name);
    }

    public static boolean saveData(Object entity, @LocalDataType int type) {
        String data = new Gson().toJson(entity);
        //64加密
        data = Base64.encodeToString(data.getBytes(), Base64.DEFAULT);
        String filePath = getFileName(type);
        return NormalUtils.saveFileInfo(filePath, data);
    }

    //获取服务器设置
    public static HttpRoomEntity getRoomData() {
        return (HttpRoomEntity) getLocalData(LOCAL_TYPE_ROOM, HttpRoomEntity.class);
    }

    private static Object getLocalData(@LocalDataType int type, Type dataType) {
        String filePath = getFileName(type);
        String data = NormalUtils.getFileInfo(filePath);
        if (TextUtils.isEmpty(data) || data.equals("null")) return null;
        data = new String(Base64.decode(data, Base64.DEFAULT));
        return new Gson().fromJson(data, dataType);
    }

    private static String getFileName(@LocalDataType int type) {
        String fileName = "core.txt";
        String tempName = "temp.txt";
        String lifeName = "life.txt";
        String filePath = ConstantCore.CORE_PATH + File.separator;
        switch (type) {
            case LOCAL_TYPE_ROOM:
                filePath = filePath + fileName;
                break;
            case LOCAL_TYPE_LIFE:
                filePath = filePath + lifeName;
                break;
            default:
                filePath = filePath + tempName;
                break;
        }
        return filePath;
    }
}
